package com.sms.rockaliyunsms.service.impl;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.sms.rockaliyunsms.service.AliyunSmsService;
import com.sms.rockaliyunsms.util.AliyunSmsSendProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 短信服务实现
 */
@Service
@Slf4j
public class AliyunSmsServiceImpl implements AliyunSmsService {

  @Override
  public String sendMessage(AliyunSmsSendProperty aliyunSmsSendProperty) {
    DefaultProfile profile = DefaultProfile.getProfile(aliyunSmsSendProperty.getRegionId(), aliyunSmsSendProperty.getAccessKeyId(), aliyunSmsSendProperty.getAccessKeySecret());
    IAcsClient client = new DefaultAcsClient(profile);

    CommonRequest request = new CommonRequest();
    request.setSysMethod(MethodType.POST);
    request.setSysDomain(aliyunSmsSendProperty.getSysDomain());
    request.setSysVersion(aliyunSmsSendProperty.getSysVersion());
    request.setSysAction(aliyunSmsSendProperty.getSysAction());
    request.putQueryParameter("RegionId", aliyunSmsSendProperty.getRegionId());
    request.putQueryParameter("PhoneNumbers", aliyunSmsSendProperty.getPhoneNumbers());
    request.putQueryParameter("SignName", aliyunSmsSendProperty.getSignName());
    request.putQueryParameter("TemplateCode", aliyunSmsSendProperty.getTemplateCode());
    request.putQueryParameter("TemplateParam", aliyunSmsSendProperty.getTemplateParam());
    try {
      CommonResponse response = client.getCommonResponse(request);
      log.info("调用短信接口信息反馈:{}",response.getData());
      return response.getData();
    } catch (ServerException e) {
      e.printStackTrace();
    } catch (ClientException e) {
      e.printStackTrace();
    }
    return null;
  }

}
